<template>
  <div>
    <FormPanel ref="formpanel1" :list="formList1" labelWidth="100" :isSubmitBtn="false" @formChange="formchangeFun" />
    <ButtonArea :style="{ paddingLeft: '100px' }">
      <el-button type="primary" @click="FormPanelSeacher">搜 索</el-button>
      <el-button @click="FormPanelreset">重置</el-button>
      <el-button :disabled="isreplace" @click="replaceFun">替换件</el-button>
    </ButtonArea>
    <FilterTable
      ref="basetable"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'rows'"
      :onColumnsChange="columns => (this.columns = columns)"
      :height="'200'"
      :dataSource="toptablelist"
      :onEnterEvent="tableDbclickFun"
      selectionType="single"
      :onRowSelectChange="selectTableFun"
      columnsRef="zjwxclhaving1"
    ></FilterTable>
    <FormPanel ref="formpanel2" :list="formList2" labelWidth="100" :isSubmitBtn="false" @formChange="formchangeFuns" />
    <FilterTable
      ref="basetables"
      :columns="columns1"
      :dataSource="tablelist"
      :params="fetchParams"
      :datakey="'rows'"
      :onColumnsChange="columns1 => (this.columns1 = columns1)"
      :height="'200'"
      :onSyncTableData="tablechangeFun"
      columnsRef="zjwxclhaving2"
    ></FilterTable>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9999,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDialog">取消</el-button>
      <el-button type="primary" @click="confirmHandle">添 加</el-button>
    </div>
  </div>
</template>
<script>
import { gethavingList, getstorelistList, getbrandsdictList, getpartInfosList, getcplistList, getaddPartList, getqueryFeeList, getqueryInsteadPartList } from '@/api/repair/business/customer/having';
export default {
  data() {
    this.basetable = null;
    this.basetables = null;
    return {
      formList1: this.createFrom(),
      formList2: this.createFroms(),
      fetchApi: gethavingList,
      fetchParams: {
        xhrAbort: true,
        sort: '',
        order: 'asc',
        limit: '10',
        offset: '0',
        queryPart_table: '',
        dmsFuncId: '35100010',
        _: '1565683718279'
      },
      getutildata: {
        dmsFuncId: '35100010',
        _: '1565683718279'
      },
      columns: this.createTable(),
      columns1: this.createTables(),
      tablelist: [],
      toptablelist: [],
      tableslist: [],
      getpartParams: {
        sort: 'STORAGE_CODE',
        order: 'asc',
        limit: '10',
        offset: '0',
        queryPart_table: '',
        dmsFuncId: '35100010',
        _: '1565746760613',
        STORAGE_CODE: '',
        PART_NO: ''
      },
      fromSubmitdata: {},
      // 是否显示替换件按钮
      isreplace: true,
      replacedata: {}
    };
  },
  created() {
    this.getlist();
  },
  mounted() {
    this.basetable = this.$refs.basetable.$refs.pageTable;
    this.basetables = this.$refs.basetables.$refs.pageTable;
  },
  methods: {
    createFrom() {
      return [
        {
          type: 'SELECT',
          label: '仓库',
          fieldName: 'STORAGE_CODE',
          itemList: []
        },
        {
          type: 'SELECT',
          label: '品牌',
          fieldName: 'BRAND',
          itemList: []
        },
        {
          type: 'SELECT',
          label: '类别',
          fieldName: 'partGroupCode',
          itemList: []
        },
        {
          type: 'INPUT',
          label: '备件代码',
          fieldName: 'partNo'
        },
        {
          type: 'INPUT',
          label: '拼音',
          fieldName: 'spellCode'
        },
        {
          type: 'INPUT',
          label: '名称',
          fieldName: 'partName'
        },
        {
          type: 'INPUT',
          label: '库位',
          fieldName: 'storagePositionCode'
        },
        {
          type: 'SELECT',
          label: '库存大于零',
          fieldName: 'stockOverZero',
          itemList: this.createDictList(1004)
        },
        {
          type: 'SELECT',
          label: '售价大于零',
          fieldName: 'priceOverZero',
          itemList: this.createDictList(1004)
        },
        {
          type: 'INPUT',
          label: '备注',
          fieldName: 'remark'
        }
      ];
    },
    createTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '仓库',
          dataIndex: 'STORAGE_CODE',
          sorter: true,
          filter: true,
          render: props => {
            return <span>{props.row.STORAGE_CODE}</span>;
          }
        },
        {
          title: '库位',
          dataIndex: 'STORAGE_POSITION_CODE',
          sorter: true,
          filter: true,
          render: props => {
            return <span>{props.row.STORAGE_POSITION_CODE}</span>;
          }
        },
        {
          title: '备件代码',
          dataIndex: 'PART_NO',
          sorter: true,
          filter: true,
          render: props => {
            return <span>{props.row.PART_NO}</span>;
          }
        },
        {
          title: '备件名称',
          dataIndex: 'PART_NAME',
          sorter: true,
          filter: true,
          render: props => {
            return <span>{props.row.PART_NAME}</span>;
          }
        },
        {
          title: '销售价',
          dataIndex: 'SALES_PRICE',
          sorter: true,
          filter: true,
          render: props => {
            return <span>{props.row.SALES_PRICE}</span>;
          }
        },
        {
          title: '成本价',
          dataIndex: 'COST_PRICE',
          sorter: true,
          filter: true,
          render: props => {
            return <span>{props.row.COST_PRICE}</span>;
          }
        },
        {
          title: '可用库存',
          dataIndex: 'USEABLE_STOCK',
          sorter: true,
          filter: true,
          render: props => {
            return <span>{props.row.USEABLE_STOCK}</span>;
          }
        },
        {
          title: '账面库存',
          dataIndex: 'STOCK_QUANTITY',
          sorter: true,
          filter: true,
          render: props => {
            return <span>{props.row.STOCK_QUANTITY}</span>;
          }
        },
        {
          title: '备注',
          dataIndex: 'REMARK',
          sorter: true,
          filter: true,
          render: props => {
            return <span>{props.row.REMARK}</span>;
          }
        },
        {
          title: '替代备件',
          dataIndex: 'OPTION_NO',
          sorter: true,
          filter: true,
          render: props => {
            return <span>{}</span>;
          }
        },
        {
          title: '故障部位代码',
          dataIndex: 'POS_CODE',
          sorter: true,
          filter: true,
          render: props => {
            return <span>{props.row.POS_CODE}</span>;
          }
        },
        {
          title: '故障部位',
          dataIndex: 'POS_NAME',
          sorter: true,
          filter: true,
          render: props => {
            return <span>{props.row.POS_NAME}</span>;
          }
        }
      ];
    },
    createFroms() {
      return [
        {
          type: 'SELECT',
          label: '仓库',
          fieldName: 'STORAGE_CODE',
          itemList: [],
          disabled: true
        },
        {
          type: 'INPUT',
          label: '备件代码',
          fieldName: 'PART_NO',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '名称',
          fieldName: 'PART_NAME',
          itemList: []
        },
        {
          type: 'SELECT',
          label: '主要备件',
          fieldName: 'IS_MAIN_PART',
          itemList: [
            { text: '是', label: '1' },
            { text: '否', label: '2' }
          ]
        },
        {
          type: 'SELECT',
          label: '价格类型',
          fieldName: 'priceType2',
          initialValue: '12361003',
          itemList: this.pricelist()
        },
        {
          type: 'INPUT',
          label: '价格系数',
          fieldName: 'PRICE_RATE',
          itemList: []
        },
        {
          type: 'SELECT',
          label: '收费区分',
          fieldName: 'CHARGE_PARTITION_CODE',
          itemList: []
        },
        {
          type: 'INPUT',
          label: '维修项目',
          fieldName: 'LABOUR_CODE',
          itemList: []
        },
        {
          type: 'INPUT_NUMBER',
          label: '发料单价',
          fieldName: 'CLAIM_PRICE',
          initialValue: '',
          change: val => {
            this.changemoney(val);
          }
        },
        {
          type: 'INPUT_NUMBER',
          label: '发料数量',
          fieldName: 'MIN_PACKAGE',
          initialValue: '1',
          change: val => {
            this.changemNum(val);
          }
        },
        {
          type: 'INPUT_NUMBER',
          label: '发料金额',
          fieldName: 'dollce',
          initialValue: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '备注',
          fieldName: 'REMARK',
          disabled: true
        }
      ];
    },
    createTables() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '销售价',
          dataIndex: 'SALES_PRICE',
          sorter: true,
          filter: true,
          render: props => {
            return <span>{props.row.SALES_PRICE}</span>;
          }
        },
        {
          title: '最低销售限价',
          dataIndex: 'MIN_LIMIT_PRICE',
          sorter: true,
          filter: true,
          render: props => {
            return <span>{props.row.MIN_LIMIT_PRICE}</span>;
          }
        },
        {
          title: '最高销售限价',
          dataIndex: 'LIMIT_PRICE',
          sorter: true,
          filter: true,
          render: props => {
            return <span>{props.row.LIMIT_PRICE}</span>;
          }
        },
        {
          title: '终端建议零售价格',
          dataIndex: 'INSURANCE_PRICE',
          sorter: true,
          filter: true,
          render: props => {
            return <span>{props.row.INSURANCE_PRICE}</span>;
          }
        },
        {
          title: '建议销售价',
          dataIndex: 'INSTRUCT_PRICE',
          sorter: true,
          filter: true,
          render: props => {
            return <span>{props.row.INSTRUCT_PRICE}</span>;
          }
        },
        {
          title: '最大库存',
          dataIndex: 'MAX_STOCK',
          sorter: true,
          filter: true,
          render: props => {
            return <span>{props.row.MAX_STOCK}</span>;
          }
        },
        {
          title: '最小库存',
          dataIndex: 'MIN_STOCK',
          sorter: true,
          filter: true,
          render: props => {
            return <span>{props.row.MIN_STOCK}</span>;
          }
        },
        {
          title: '账面库存',
          dataIndex: 'STOCK_QUANTITY',
          sorter: true,
          filter: true,
          render: props => {
            return <span>{props.row.STOCK_QUANTITY}</span>;
          }
        },
        {
          title: '可用库存',
          dataIndex: 'USEABLE_STOCK',
          sorter: true,
          filter: true,
          render: props => {
            return <span>{props.row.USEABLE_STOCK}</span>;
          }
        },
        {
          title: '借进数量',
          dataIndex: 'BORROW_QUANTITY',
          sorter: true,
          filter: true,
          render: props => {
            return <span>{props.row.BORROW_QUANTITY}</span>;
          }
        },
        {
          title: '借出数量',
          dataIndex: 'LEND_QUANTITY',
          sorter: true,
          filter: true,
          render: props => {
            return <span>{props.row.LEND_QUANTITY}</span>;
          }
        },
        {
          title: '锁定量',
          dataIndex: 'LOCKED_QUANTITY',
          sorter: true,
          filter: true,
          render: props => {
            return <span>{props.row.LOCKED_QUANTITY}</span>;
          }
        },
        {
          title: '替代备件',
          dataIndex: 'OPTION_NO',
          sorter: true,
          filter: true,
          render: props => {
            return <span>{props.row.OPTION_NO ? props.row.OPTION_NO : ''}</span>;
          }
        },
        {
          title: '备件类别',
          dataIndex: 'leibie',
          sorter: true,
          filter: true,
          render: props => {
            return <span>{}</span>;
          }
        },
        {
          title: '单位',
          dataIndex: 'UNIT_NAME',
          sorter: true,
          filter: true,
          render: props => {
            return <span>{props.row.UNIT_NAME}</span>;
          }
        },
        {
          title: '车型',
          dataIndex: 'PART_MODEL_GROUP_CODE_SET',
          sorter: true,
          filter: true,
          render: props => {
            return <span>{props.row.PART_MODEL_GROUP_CODE_SET}</span>;
          }
        }
      ];
    },
    // 获取数据字典数据
    async getlist() {
      this.formList1[0].itemList = (await getstorelistList(this.getutildata)).map(x => ({ text: x.STORAGE_NAME, value: x.STORAGE_CODE }));
      this.formList1[1].itemList = (await getbrandsdictList(this.getutildata)).map(x => ({ text: x.BRAND_CODE, value: x.BRAND_NAME }));
      this.formList1[2].itemList = (await getpartInfosList(this.getutildata)).map(x => ({ text: x.PART_MODEL_GROUP_NAME, value: x.PART_MODEL_GROUP_CODE }));
      this.formList2[6].itemList = (await getqueryFeeList(this.getutildata)).map(x => ({ text: x.MANAGE_SORT_NAME, value: x.MANAGE_SORT_CODE }));
    },
    // 重置
    FormPanelreset() {
      this.$refs.formpanel1.RESET_FORM();
    },
    FormPanelSeacher() {
      this.$refs.formpanel1.SUBMIT_FORM();
    },
    async replaceFun() {
      let json = {
        sort: '',
        order: 'asc',
        limit: '10',
        offset: '0',
        queryPart_table: '',
        dmsFuncId: '35100010',
        _: '1565774420453',
        PART_NO: this.replacedata.OPTION_NO
      };
      let res = await getqueryInsteadPartList(json);
      this.toptablelist.forEach(x => {
        res.forEach(y => {
          if (y.PART_NO == x.PART_NO) {
            this.toptablelist[x] = res[y];
          }
        });
      });
      console.log(res);
    },
    // 选中事件
    selectTableFun(val) {
      console.log(val);

      console.log(val, 'val');
      if (val && val.length > 0) {
        let value = val[0].OPTION_NO;
        this.replacedata = val[0];
        if (value == null) {
          this.isreplace = true;
        } else {
          this.isreplace = false;
        }
      }
    },
    formchangeFun(val) {
      console.log(val);
      this.fetchParams = Object.assign({}, this.fetchParams, val, { xhrAbort: false });
    },
    formchangeFuns(val) {
      console.log(val);
      this.fromSubmitdata = val; // {...val};
    },
    // 价格类型数据字典
    pricelist() {
      return [
        {
          label: '12361001',
          text: '成本价'
        },
        {
          label: '12361002',
          text: '销售价'
        },
        {
          label: '12361003',
          text: '索赔价'
        },
        {
          label: '12361004',
          text: '网点价'
        },
        {
          label: '12361005',
          text: '最新进货价'
        },
        {
          label: '12361006',
          text: '含税成本价'
        },
        {
          label: '12361007',
          text: '终端建议零售价格'
        },
        {
          label: '12361008',
          text: '建议销售价'
        }
      ];
    },
    tableDbclickFun(val) {
      console.log(val, '双击');
      // this.tablelist = val;
      this.formList2[0].initialValue = val.STORAGE_CODE;
      this.formList2[1].initialValue = val.UNIT_CODE;
      this.formList2[2].initialValue = val.PART_NAME;
      this.formList2[8].initialValue = val.CLAIM_PRICE;
      this.formList2[10].initialValue = val.CLAIM_PRICE;
      // this.formList2[0].initialValue=val.SALES_PRICE;
      // this.formList2[0].initialValue=val.STORAGE_CODE;
      // this.formList2[0].initialValue=val.STORAGE_CODE;
      this.getpartlist(val);
    },
    async getpartlist(val) {
      this.getpartParams.STORAGE_CODE = val.STORAGE_CODE;
      this.getpartParams.PART_NO = val.PART_NO;
      let res = (await getaddPartList(this.getpartParams)).data.rows;
      this.basetables.RESET_TABLE_DATA();
      this.basetables.EXECUTE_INSERT(res);
    },
    changemoney(val) {
      console.log(val, 'input');
      let num = this.formList2[9].initialValue;
      this.formList2[10].initialValue = val * num;
    },
    changemNum(val) {
      console.log(val, '改变');
      let money = this.formList2[8].initialValue;
      this.formList2[10].initialValue = val * money;
    },
    // 关闭弹窗
    closeDialog() {
      this.$emit('close', false);
    },
    tablechangeFun(val) {
      console.log(val);
      this.tableslist = val;
    },
    // 添加事件提交表单
    confirmHandle() {
      let json = this.tableslist[0];
      this.$refs.formpanel2.SUBMIT_FORM();
      console.log(json, this.fromSubmitdata);

      let formdata = Object.assign({}, this.fromSubmitdata, json);

      console.log(formdata);
      if (formdata.dollce < formdata.COST_PRICE) {
        this.$confirm('该备件发料价格低于成本价格，是否继续添加备件？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            this.$emit('changedata', formdata);
          })
          .catch(() => {
            // this.$notify.info({title: '提示信息',message:'已取消删除'});
          });
      } else {
        this.$emit('changedata', formdata);
      }
    }
  }
};
</script>
